package cn.com.w.web.controller;

import cn.com.w.common.VO.BaseVO;
import cn.com.w.common.VO.PageVO;
import cn.com.w.common.param.BlogParam;
import cn.com.w.common.param.PageParam;
import cn.com.w.dao.Entity.TBlog;
import cn.com.w.dao.Service.ITBlogService;
import cn.com.w.secutity.Security.JwtTokenUtils;
import cn.com.w.web.Handler.BlogHandler;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/blog")
@Api(tags = "博客维护")
public class BlogController {

    private final ITBlogService itBlogService;
    private final JwtTokenUtils jwtTokenUtils;
    private final BlogHandler blogHandler;

    public BlogController(ITBlogService blogService, JwtTokenUtils jwtTokenUtils, BlogHandler blogHandler) {
        this.itBlogService = blogService;
        this.jwtTokenUtils = jwtTokenUtils;
        this.blogHandler = blogHandler;
    }

    @GetMapping("/list")
    public BaseVO<?> listBlog(@RequestHeader(value = "Authorization") String token, PageParam param) {
        QueryWrapper<TBlog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        IPage<TBlog> page = new Page<>(param.getPage(), param.getLimit());
        IPage<TBlog> blogPage = itBlogService.page(page, wrapper);
        List<TBlog> records = blogPage.getRecords();
        return BaseVO.builder()
                .code(200)
                .content(PageVO.builder()
                        .limit(param.getLimit())
                        .data(records)
                        .total(blogPage.getTotal())
                        .page(param.getPage())
                        .build())
                .build();
    }

    @PostMapping("/save")
    public BaseVO<Boolean> saveBlog(@RequestHeader(value = "Authorization") String token, @RequestBody BlogParam param) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(blogHandler.saveBlog(param, token))
                .build();
    }

    @PostMapping("/delete")
    public BaseVO<Boolean> deleteBlog(@RequestHeader(value = "Authorization") String token, @RequestParam Long id) {
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(itBlogService.removeById(id))
                .build();
    }

    @PostMapping("/update")
    public BaseVO<Boolean> updateBlog(@RequestHeader(value = "Authorization") String token, @RequestBody TBlog blog) {
        Long userId = jwtTokenUtils.getUserIdFromToken(token);
        blog.setUpdateTime(LocalDateTime.now());
        blog.setUserId(userId);
        return BaseVO.<Boolean>builder()
                .code(200)
                .content(itBlogService.updateById(blog))
                .build();
    }


}
